package com.artron.mediaartron.ui.helper;

import android.graphics.Paint;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.artron.baselib.utils.DensityUitls;

import java.lang.reflect.Field;

;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-07-17.
 */

public class TabLayoutHelper {

    private static final String TAG = "TabLayoutHelper";

    public static void setTabViewMargin(final TabLayout tabLayout) {
//        tabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                try {
        //拿到tabLayout的mTabStrip属性
//                    Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
//                    mTabStripField.setAccessible(true);
//
//                    LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);
//
//                    int dp20 = DensityUitls.dip2px(20);
//
//                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
//                        View tabView = mTabStrip.getChildAt(i);
//                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
//                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
////                        params.width = width ;
//                        params.leftMargin = dp20;
//                        params.rightMargin = dp20;
//                        tabView.setLayoutParams(params);
//                    }
//
//                } catch (NoSuchFieldException | IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        tabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                LinearLayout parent = (LinearLayout) tabLayout.getChildAt(0);
//                for (int x = 0; x < parent.getChildCount(); x++) {
//                    View child = parent.getChildAt(x);
//                    Log.d(TAG, "width: " + child.getWidth());
//                    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)
//                            child.getLayoutParams();
//                    int dp20 = DensityUitls.dip2px(20);
//                    layoutParams.leftMargin = dp20;
//                    layoutParams.rightMargin = dp20;
//                    child.setLayoutParams(layoutParams);
//                }
//            }
//        });

        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = DensityUitls.dip2px(20);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = mTextView.getWidth();
                        Paint paint = new Paint();
                        paint.setTextSize(mTextView.getTextSize());
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        float widthText = paint.measureText(mTextView.getText().toString());
                        if (widthText != 0 && widthText < width) {
                            width = (int) (widthText + 10);
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
